<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>会员等级添加/修改</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('style.css,base.css,base_new.css,equity.css'); }
    {php echo Common::getScript("uploadify/jquery.uploadify.min.js,product_add.js,choose.js,imageup.js,template.js");}
    {php echo Common::getCss('uploadify.css','js/uploadify/'); }
</head>
<body>
<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td" valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td ">

            <form method="post" name="product_frm" id="product_frm">
                <div class="manage-nr">
                    <div class="cfg-header-bar" id="nav">
                        <div class="cfg-header-tab">
                            <span class="item on" data-action="basic_info"><s></s>基本资料</span>
                            <span class="item" data-action="grade_equity"><s></s>等级权益</span>
                            <span class="item" data-action="style_select"><s></s>样式选择</span>
                            <span class="item" data-action="grade_explain"><s></s>详细说明</span>
                        </div>

                        <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
                    </div>
                    <!--基础信息开始-->
                    <div class="clear clear-show basic_info">
                        <input type="hidden" name="is_system" value="0">
                        <ul class="info-item-block">
                            <li>
                                <span class="item-hd">会员等级名称：</span>
                                <div class="item-bd">
                                    <input class="input-text w150" type="input" id="name"  name="name" value="{$info['name']}"/> <span class="red" id="name_err"></span>
                                </div>
                            </li>
                            <li>
                                <span class="item-hd">升级条件{Common::get_help_icon('member_grade_rule')}：</span>
                                <div class="item-bd">
                                    <label class="radio-label mr-10">
                                        <input onclick="grade_rules_switch(1)" {if $grade_rule==3}disabled{/if} name="grade_rules" type="radio" {if $info['is_system']==1 || $info['grade_rules']==1}checked{/if} value="1">积分区间
                                    </label>
                                    {if St_Functions::is_normal_app_install('system_wallet')}
                                    <label class="radio-label mr-10">
                                        <input onclick="grade_rules_switch(2)" {if $grade_rule!=3}disabled{/if} name="grade_rules" type="radio" {if $info['grade_rules']==2}checked{/if} value="2">充值升级
                                    </label>
									{/if}
                                    <div class="mt-10 grade_rules grade_rules_1 {if $info['grade_rules']!=1}hide{/if}">
                                        <input class="input-text w150 {if $info['is_system']==1}c-999{/if}" type="input" id="begin" onkeyup="this.value=this.value.replace(/\D/g,'')" {if $info['is_system']==1}disabled{/if} name="begin" value="{$info['begin']}"/> ~
                                        <input class="input-text w150" type="input" id="end" name="end" value="{$info['end']}" onkeyup="this.value=this.value.replace(/\D/g,'')"/>
                                        <span class="red" id="name_err"></span>
                                    </div>
                                    <div class="mt-10 grade_rules grade_rules_2 {if $info['grade_rules']!=2}hide{/if}">
                                        <div>
                                            {if $info['is_system']==1}
                                            <span class="item-text">默认等级无需充值</span>
                                            <input type="hidden" name="price" value="0" class="input-text w80" >
                                            {else}
                                            <span class="item-text">累计充值</span>
                                            <input type="text" name="price" value="{$info['price']}" class="input-text w80" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            <span class="item-text">元，可升级到该级别</span>
                                            {/if}
                                        </div>
                                    </div>
                                    <div class="expiry-date-container mt-5" >
                                        <div class="mt-10">
                                            <span class="item-text">有效期：</span>
                                            <label class="radio-label mr-10">
                                                <input name="valid_type" onclick="valid_type_switch(1)" type="radio" value="1" {if $info['valid_type']==1}checked{/if}>永久有效
                                            </label>
                                            {if $info['is_system']!=1}
                                            <label class="radio-label mr-10">
                                                <input name="valid_type" onclick="valid_type_switch(2)" type="radio" value="2" {if $info['valid_type']==2}checked{/if}>有效期
                                            </label>
                                            <span class="valid_type {if $info['valid_type']!=2}hide{/if}">
                                                <input type="text" name="expiration_time" class="input-text w80" value="{$info['expiration_time']}">
                                                天，到期降至指定等级
                                                <span class="select-box w100">
                                                    <select name="grade_id" class="select">
                                                        {loop $grade_list $v}
                                                        {if $info['grade_id']}
                                                        <option {if $info['grade_id']==$v['id']}selected{/if} value="{$v['id']}">{$v['name']}</option>
                                                        {else}
                                                        <option {if $v['begin']==0}selected{/if} value="{$v['id']}">{$v['name']}</option>
                                                        {/if}
                                                        {/loop}
                                                    </select>
                                                </span>
                                            </span>
                                            {/if}
                                        </div>
                                    </div>
                                    <script>
                                        function valid_type_switch(type)
                                        {
                                            if(type==1){
                                                $('.valid_type').addClass('hide');
                                            }else{
                                                $('.valid_type').removeClass('hide');
                                            }
                                        }
                                        function grade_rules_switch(type)
                                        {
                                            $('.grade_rules').addClass('hide');
                                            $('.grade_rules_'+type).removeClass('hide');
                                        }
                                    </script>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="clear clear-show grade_equity hide">
                        <ul class="info-item-block">
                            <li>
                                <span class="item-hd">权益选择：</span>
                                <div class="item-bd">
                                    {loop $ty_equity $v}
                                    {if $v['title']=='送积分'}
                                    <div class="mb-10">
                                        <label class="check-label">
                                            <input type="checkbox" name="equity_id[]" value="{$v['id']}" {if in_array($v['id'],$grade_equity['equity_id'])}checked{/if}>送
                                            <input type="text" name="gift_jifen" value="{$grade_equity['gift_jifen']}" class="input-text w80">积分
                                        </label>
                                    </div>
                                    {/if}
                                    {if $v['title']=='会员折扣'}
                                    <div class="mb-10">
                                        <label class="check-label">
                                            <input type="checkbox" name="equity_id[]" value="{$v['id']}" {if in_array($v['id'],$grade_equity['equity_id'])}checked{/if}>会员折扣
                                        </label>
                                    </div>
                                    {/if}
                                    {if St_Functions::is_normal_app_install('coupon')}
                                    {if $v['title']=='生日券'}
                                    <div class="mb-10">
                                        <label class="check-label w100">
                                            <input type="checkbox" name="equity_id[]" value="{$v['id']}" {if in_array($v['id'],$grade_equity['equity_id'])}checked{/if}>生日券
                                        </label>
                                        <input data-id="{$grade_equity['birthday_coupon_id']}" class="btn radius btn-secondary size-S" type="button" onclick="coupon_select('birthday_coupon_id',$(this))" value="选择优惠券" style="margin-bottom: 3px">
                                        <table class="table table-border table-bordered w800 mt-10">
                                            <thead>
                                            <tr>
                                                <th class="text-c">编号</th>
                                                <th class="text-l">名称</th>
                                                <th class="text-c">有效期</th>
                                                <th class="text-c">赠送数量</th>
                                                <th class="text-c">操作</th>
                                            </tr>
                                            </thead>
                                            <tbody class="birthday_coupon_id">
                                            {if $grade_equity['birthday_coupon_id']}
                                            {loop $grade_equity['birthday_coupon'] $bir}
                                                <tr>
                                                    <td class="text-c">
                                                        {$bir['id']}
                                                        <input type="hidden" name="birthday_coupon_id[]" value="{$bir['id']}">
                                                    </td>
                                                    <td class="text-l">{$bir['name']}</td>
                                                    <td class="text-c">
                                                        {if $bir['isnever']}
                                                        {date('Y-m-d H:i:s',$bir['starttime'])} 至 {date('Y-m-d H:i:s',$bir['endtime'])}
                                                        {else}
                                                        永久有效
                                                        {/if}</td>
                                                    <td class="text-c">
                                                        <input type="text" name="birthday_coupon_num[{$bir['id']}]" value="{$grade_equity['birthday_coupon_num'][$bir['id']]}" class="input-text w80 set_coupon_num" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                                    </td>
                                                    <td class="text-c">
                                                        <a href="javascript:;" onclick="$(this).parent().parent().remove();" class="delete">删除</a>
                                                    </td>
                                                </tr>
                                            {/loop}
                                            {else}
                                                <tr class="prompt">
                                                    <td class="text-c" colspan="5">还未选择优惠券</td>
                                                </tr>
                                            {/if}
                                            </tbody>
                                        </table>
                                    </div>
                                    {/if}
                                    {if $v['title']=='升级有礼'}
                                    <div class="mb-10">
                                        <label class="check-label w100">
                                            <input type="checkbox" name="equity_id[]" value="{$v['id']}" {if in_array($v['id'],$grade_equity['equity_id'])}checked{/if}>升级有礼
                                        </label>
                                        <input data-id="{$grade_equity['upgrade_coupon_id']}" class="btn radius btn-secondary size-S" type="button" onclick="coupon_select('upgrade_coupon_id',$(this))" value="选择优惠券" style="margin-bottom: 3px">
                                        <table class="table table-border table-bordered w800 mt-10">
                                            <thead>
                                            <tr>
                                                <th class="text-c">编号</th>
                                                <th class="text-l">名称</th>
                                                <th class="text-c">有效期</th>
                                                <th class="text-c">赠送数量</th>
                                                <th class="text-c">操作</th>
                                            </tr>
                                            </thead>
                                            <tbody class="upgrade_coupon_id">
                                            {if $grade_equity['upgrade_coupon_id']}
                                            {loop $grade_equity['upgrade_coupon'] $k $upg}
                                                <tr>
                                                    <td class="text-c">
                                                        {$upg['id']}
                                                        <input type="hidden" name="upgrade_coupon_id[]" value="{$upg['id']}">
                                                    </td>
                                                    <td class="text-l">{$upg['name']}</td>
                                                    <td class="text-c">
                                                        {if $upg['isnever']}
                                                        {date('Y-m-d H:i:s',$upg['starttime'])} 至 {date('Y-m-d H:i:s',$upg['endtime'])}
                                                        {else}
                                                        永久有效
                                                        {/if}</td>
                                                    <td class="text-c">
                                                        <input type="text" name="upgrade_coupon_num[{$upg['id']}]" value="{$grade_equity['upgrade_coupon_num'][$upg['id']]}" class="input-text w80">
                                                    </td>
                                                    <td class="text-c">
                                                        <a href="javascript:;" onclick="$(this).parent().parent().remove();" class="delete">删除</a>
                                                    </td>
                                                </tr>
                                            {/loop}
                                            {else}
                                                <tr class="prompt">
                                                    <td class="text-c" colspan="5">还未选择优惠券</td>
                                                </tr>
                                            {/if}
                                            </tbody>
                                        </table>
                                    </div>
                                    {/if}
                                    {if $v['title']=='新人权益礼包' && $info['is_system']==1}
                                    <div class="mb-10">
                                        <label class="check-label w100">
                                            <input type="checkbox" name="equity_id[]" value="{$v['id']}" {if in_array($v['id'],$grade_equity['equity_id'])}checked{/if}>新人权益礼包
                                        </label>
                                        <input data-id="{$grade_equity['new_user_coupon_id']}" class="btn radius btn-secondary size-S" type="button" onclick="coupon_select('new_user_coupon_id',$(this))" value="选择优惠券" style="margin-bottom: 3px">
                                        <table class="table table-border table-bordered w800 mt-10">
                                            <thead>
                                            <tr>
                                                <th class="text-c">编号</th>
                                                <th class="text-l">名称</th>
                                                <th class="text-c">有效期</th>
                                                <th class="text-c">赠送数量</th>
                                                <th class="text-c">操作</th>
                                            </tr>
                                            </thead>
                                            <tbody class="new_user_coupon_id">
                                            {if $grade_equity['new_user_coupon_id']}
                                            {loop $grade_equity['new_user_coupon'] $k $new}
                                                <tr>
                                                    <td class="text-c">
                                                        {$new['id']}
                                                        <input type="hidden" name="new_user_coupon_id[]" value="{$new['id']}">
                                                    </td>
                                                    <td class="text-l">{$new['name']}</td>
                                                    <td class="text-c">
                                                        {if $new['isnever']}
                                                        {date('Y-m-d H:i:s',$new['starttime'])} 至 {date('Y-m-d H:i:s',$new['endtime'])}
                                                        {else}
                                                        永久有效
                                                        {/if}</td>
                                                    <td class="text-c">
                                                        <input type="text" name="new_user_coupon_num[{$new['id']}]" value="{$grade_equity['new_user_coupon_num'][$new['id']]}" class="input-text w80">
                                                    </td>
                                                    <td class="text-c">
                                                        <a href="javascript:;" onclick="$(this).parent().parent().remove();" class="delete">删除</a>
                                                    </td>
                                                </tr>
                                            {/loop}
                                            {else}
                                                <tr class="prompt">
                                                    <td class="text-c" colspan="5">还未选择优惠券</td>
                                                </tr>
                                            {/if}
                                            </tbody>
                                        </table>
                                    </div>
                                    {/if}
                                    {/if}
                                    {if St_Functions::is_normal_app_install('integral_award')}
                                    {if $v['title']=='免费抽奖'}
                                    <div class="mb-10">
                                        <label class="check-label">
                                            <input type="checkbox" name="equity_id[]" value="{$v['id']}" {if in_array($v['id'],$grade_equity['equity_id'])}checked{/if}>免费积分抽奖
                                            <input type="text" name="lottery_num" value="{$grade_equity['lottery_num']}" class="input-text w80">次
                                        </label>
                                    </div>
                                    {/if}
                                    {/if}
                                    {/loop}
                                    <div class="mb-10 w800">
                                        {loop $zdy_equity $v}
                                        <label class="check-label mr-10 w100">
                                            <input type="checkbox" name="equity_id[]" value="{$v['id']}" {if in_array($v['id'],$grade_equity['equity_id'])}checked{/if}>{$v['title']}
                                        </label>
                                        {/loop}
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="clear clear-show style_select hide">
                        <ul class="info-item-block">
                            <li>
                                <span class="item-hd">选择等级封面图：</span>
                                <div class="item-bd">
                                    {loop $style_img $k $img}
                                    <div class="style-container fl">
                                        <label class="radio-label">
                                            <img class="style-img" src="{$img}">
                                            <input type="radio" name="style_id" value="{$k}" {if $grade_equity['style_id']==$k || $k==1}checked{/if}>
                                        </label>
                                    </div>
                                    {/loop}
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="clear clear-show grade_explain hide">
                        <ul class="info-item-block">
                            <li>
                                <span class="item-hd">等级说明：</span>
                                <div class="item-bd">
                                    {php Common::getEditor('content',$grade_equity['content'],800,600);}
                                </div>
                            </li>
                        </ul>
                    </div>
                    <!--/基础信息结束-->
                    <div class="clear clearfix mt-5">
                        <input type="hidden" name="id" id="id" value="{$info['id']}"/>
                        <input type="hidden" name="action" id="action" value="{$action}"/>
                        <a class="btn btn-primary radius size-L ml-115" id="btn_save" href="javascript:;">保存</a>
                    </div>

                </div>
            </form>
        </td>
    </tr>
</table>

<script>
    var grade_rule = {$grade_rule};
    function coupon_select(type,obj)
    {
        var select_id = $(obj).attr('data-id');
        var url = SITEURL+'membergrade/dialog_coupon_list?type='+type+'&select_id='+select_id;
        CHOOSE.setSome(
            "选择优惠券",
            {width:900,loadWindow:window,loadCallback:coupon_show},
            url,
            1
        )
    }

    function coupon_show(res,status)
    {
        var id = res.id;
        var name = res.name;
        var validity_period = res.validity_period;
        var html = '';
        var key = '';
        if(res.type=='birthday_coupon_id'){
            key = 'birthday_coupon_num';
        }else if(res.type=='upgrade_coupon_id'){
            key = 'upgrade_coupon_num';
        }else{
            key = 'new_user_coupon_num';
        }

        for(var i=0;i<res.id.length;i++){
            html += '<tr>';
            html += '<td class="text-c">'+id[i]+'<input type="hidden" name="'+res.type+'[]" value="'+id[i]+'"></td>';
            html += '<td class="text-l">'+name[i]+'</td>';
            html += '<td class="text-c">'+validity_period[i]+'</td>';
            html += '<td class="text-c"><input type="text" name="'+key+'['+id[i]+']" value="1" class="input-text w80 set_coupon_num"></td>';
            html += '<td class="text-c">';
            html += '<a href="javascript:;" onclick="$(this).parent().parent().remove();" class="delete">删除</a>';
            html += '</td>';
            html += '</tr>';
        }
        $("."+res.type).append(html);
        $("."+res.type+' .prompt').remove();
        $("body").on("keyup", ".set_coupon_num", function() {
            $(this).val($(this).val().replace(/\D/g, ''));
        });
    }

    var action = "{$action}";
    $(document).ready(function () {
        $('.cfg-header-tab span').on('click',function(){
            var action = $(this).attr('data-action');
            $('.cfg-header-tab span').removeClass('on');
            $(this).addClass('on');
            $('.clear-show').addClass('hide');
            $('.'+action).removeClass('hide');
        });

        //保存
        $("#btn_save").click(function () {
            if($('#name').val()==''){
                ST.Util.showMsg('请填写“会员等级名称”', '3', 2000);
                return false;
            }
            if(grade_rule==3){
                if($("[name='price']").val()==''){
                    ST.Util.showMsg('请填写“累计金额”', '3', 2000);
                    return false;
                }
            }else{
                if($('#begin').val()==''){
                    ST.Util.showMsg('请填写“积分区间”', '3', 2000);
                    return false;
                }
                if($('#end').val()=='' ){
                    ST.Util.showMsg('请填写“积分区间”', '3', 2000);
                    return false;
                }
            }
            var valid_type = $("input[name='valid_type']:checked").val();
            if(valid_type==2){
                if($("[name='expiration_time']").val()==''){
                    ST.Util.showMsg('请填写“有效期”', '3', 2000);
                    return false;
                }
            }
            var status = false;
            $(".set_coupon_num").each(function(){
                if($(this).val()<1){
                    status = true;
                }
            });
            if(status){
                ST.Util.showMsg('请填写“优惠券的赠送数量！”', '3', 2000);
                return false;
            }

            $.ajaxform({
                url: SITEURL + "membergrade/ajax_save",
                method: "POST",
                form: "#product_frm",
                dataType: "JSON",
                success: function (data) {
                    if (data.status) {
                        ST.Util.showMsg('保存成功!', '4', 2000);
                        setTimeout(function(){
                            window.location.reload();
                        },1000)
                    }else{
                        ST.Util.showMsg(data.msg, '3', 2000);
                    }
                }});
        });
    });
</script>

</body>
</html>